//
//  CLHttpTool.m
//  togetherLBS
//
//  Created by louis on 15/7/28.
//  Copyright (c) 2015年 celi. All rights reserved.
//

#import "CLHttpTool.h"
#import "CLEncoder.h"

extern NSString * AFQueryStringFromParametersWithEncoding(NSDictionary *parameters, NSStringEncoding stringEncoding);

@interface CLHttpTool()
/**
 *  http请求使用的网络请求管理者
 */
@property (nonatomic, strong) AFHTTPSessionManager *httpManager;
/**
 *  当前http请求的操作
 */
@property (nonatomic, strong) NSURLSessionDataTask *currentRequestOp;
@end

@implementation CLHttpTool
/**
 *  单例类的实现
 */
SingletonM(HttpTool)

/**
 *  支持https的传输协议
 */
- (AFHTTPSessionManager *)httpManager
{
    if (_httpManager == nil) {
        _httpManager = [AFHTTPSessionManager manager];
        _httpManager.requestSerializer.timeoutInterval = kTIME_OUT;
        AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];
        securityPolicy.allowInvalidCertificates = YES;
        securityPolicy.validatesDomainName = NO;
        _httpManager.securityPolicy = securityPolicy;

    }
    return _httpManager;
}


- (void)getWithUrl:(NSString *)url paramaters:(NSDictionary *)params success:(void (^)(id responseObject))success failure:(void (^)(NSError *))failure
{
    self.httpManager.responseSerializer = [AFJSONResponseSerializer serializer];
    self.currentRequestOp = [self.httpManager GET:url parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        NSLog(@"%lld", downloadProgress.totalUnitCount);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}

- (void)getImageWithUrl:(NSString *)url paramaters:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    self.httpManager.responseSerializer = [AFImageResponseSerializer serializer];
    
    self.currentRequestOp = [self.httpManager GET:url parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        NSLog(@"%lld", downloadProgress.totalUnitCount);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}

- (void)getVoiceDataWithUrl:(NSString *)url success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    self.httpManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    self.httpManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/octet-stream", @"audio/basic", nil];
    
    self.currentRequestOp = [self.httpManager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        NSLog(@"%lld", downloadProgress.totalUnitCount);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}

- (void)postWithUrl:(NSString *)url paramaters:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    self.httpManager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    self.currentRequestOp = [self.httpManager POST:url parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        DLog(@"%lld", downloadProgress.totalUnitCount);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}

- (void)postWithUrl:(NSString *)url paramaters:(NSDictionary *)params formDataArray:(NSArray *)formDataArray success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    self.httpManager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    self.currentRequestOp = [self.httpManager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull totalFormData) {
        [formDataArray enumerateObjectsUsingBlock:^(CLFormData *formData, NSUInteger idx, BOOL *stop) {
            [totalFormData appendPartWithFileData:formData.data name:formData.name fileName:formData.filename mimeType:formData.mimeType];
        }];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
         DLog(@"%lld", uploadProgress.totalUnitCount);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}

- (void)cancelCurrentRequest
{
    if (self.currentRequestOp) {
        [self.currentRequestOp cancel];
    }
}

- (void)cancelAllRequest
{
    if (_httpManager) {
        [self.httpManager.operationQueue cancelAllOperations];
    }
}

@end
